import Vue from 'vue';
import store from 'vuex';
import { formatTen } from '../assets/js/help.js';

//毫秒转日期  yyyy-MM-dd hh:mm:ss
Vue.filter('mileSecondToDate', function(value) {
	let result = '';
	if (value) {
		let date = new Date(value);
		result =
			date.getFullYear() +
			'-' +
			formatTen(date.getMonth() + 1) +
			'-' +
			formatTen(date.getDate()) +
			' ' +
			formatTen(date.getHours()) +
			':' +
			formatTen(date.getMinutes()) +
			':' +
			formatTen(date.getSeconds());
	}
	return result;
});

//分钟转 XX天XX时XX分
Vue.filter('minutesToHowLong', function(Val) {
	let mits = Val < 0 ? 0 : Val;
	//计算天数
	let days = Math.floor(mits / (60 * 24));

	//计算小时数
	let leave_d = mits % (60 * 24); //计算除去整天数后还剩下的分钟数
	let hours = Math.floor(leave_d / 60);

	//计算分钟数
	let minutes = leave_d % 60;

	if (days !== 0) {
		return '' + days + '天' + hours + '小时' + minutes + '分钟';
	} else if (hours !== 0) {
		return '' + hours + '小时' + minutes + '分钟';
	} else {
		return '' + minutes + '分钟';
	}
});

Vue.filter('formatMoney', function(value) {
	if (value == null) {
		return ' ';
	}
	if (!value) {
		return '0.00';
	}
	if (value == 0) {
		return '0.00';
	}

	function formatCurrency(num) {
		num = num.toString().replace(/\$|\,/g, '');
		if (isNaN(num)) num = '0';
		var sign = num == (num = Math.abs(num));
		num = Math.floor(num * 100 + 0.50000000001);
		var cents = num % 100;
		num = Math.floor(num / 100).toString();
		if (cents < 10) cents = '0' + cents;
		// for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		//     num = num.substring(0, num.length - (4 * i + 3)) + ',' +
		//         num.substring(num.length - (4 * i + 3));
		return (sign ? '' : '-') + num + '.' + cents;
	}
	//console.log(formatCurrency(value))
	return formatCurrency(value);
});

Vue.filter('formatTimeLength', function(value) {
	//console.log(parkingDurationShow)
	if (value == null) return;
	var theMinute = parseInt(value) + 1;
	var theHour = 0;
	var theDay = 0;
	if (theMinute > 60) {
		theHour = Math.floor(theMinute / 60);
		theMinute = Math.floor(theMinute % 60);
	}
	if (theHour > 24) {
		theDay = Math.floor(theHour / 24);
		theHour = Math.floor(theHour % 24);
	}
	var result = '';
	if (theMinute > 0) {
		result = theMinute + '分钟' + result;
	}
	if (theHour > 0) {
		result = theHour + '小时' + result;
	}
	if (theDay > 0) {
		result = theDay + '天' + result;
	}
	if (result == '') {
		return '小于1分钟';
	}
	return result;
});

//拼接图片路径
Vue.filter('setImgPath', function(path) {
	return path ? (path.indexOf('/img') > -1 ? path : '' + path) : '';
});
